package com.app.micaihu.view.army.activity

import android.os.Bundle
import android.view.View
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.contract.ArmyDetailContract
import com.app.micaihu.view.army.dialog.ArmyCreateSuccessDialog
import com.app.micaihu.view.army.dialog.ArmyUpgradeSuccessDialog
import com.app.micaihu.view.army.presenter.ArmyDetailPresenter
import com.app.micaihu.view.bean.ArmyDetail
import com.app.micaihu.view.bean.ArmyUpgrade
import com.app.micaihu.view.user.userinfo.activity.WebActivity
import com.app.micaihu.view.utils.loadImage
import com.app.utils.biz.Biz
import com.app.widget.CommonTitle
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.army_level_activity.*

/**
 * 军团等级详情
 */
class ArmyLevelActivity : BaseMvpActivity<ArmyDetailContract.View, ArmyDetailPresenter>(), ArmyDetailContract.View {
    override fun getPresenter() = ArmyDetailPresenter()

    override fun getLayoutId() = R.layout.army_level_activity

    override fun getBundle(bundle: Bundle) {
        gid = bundle.getString(Config.Extra.id, "")
        toUid = bundle.getString(Config.Extra.toUid, "")
    }

    private var gid = ""
    private var toUid = ""
    private var lelveH5 = ""
    private var armyGroupUpgradeStatus = false

    private val armyUpgradeSuccessDialog: ArmyUpgradeSuccessDialog by lazy {
        ArmyUpgradeSuccessDialog()
    }

    override fun setData() {
        mCommonTitle.setTextConfirm("等级详情")
        mPresenter?.detail(toUid, gid)
    }

    override fun addListeners() {
        mCommonTitle.setCommonTitleCallBackListener(object : CommonTitle.CommonTitleCallBackListener {
            override fun btnBackOnClick() {
                finish()
            }

            override fun btnConfirmOnClick() {
                val bundle = Bundle()
                bundle.putString(Config.Extra.url, lelveH5)
                enterPager(WebActivity::class.java, bundle)
            }

        })

        tvUpgrade.setOnClickListener {
            if (armyGroupUpgradeStatus) {
                mPresenter?.armyUpgrade(gid)
            } else {
                ToastUtils.showShort("暂未达到升级条件")
            }
        }
    }

    override fun handleDetail(result: BaseBean<ArmyDetail?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                result.data?.let { detail ->
                    detail.armyGroupInfo?.let {
                        ivArmyIcon.loadImage(it.icon)
                        tvArmyTitle.text = it.title
                        tvArmyNumber.text = "番号 " + it.id
                        tvArmyLevel.text = it.level + " >"
                        tvArmyZone.text = it.war_zone
                        if (Biz.instance.uid() == it.originator) {
                            cardViewUpgrade.visibility = View.VISIBLE
                        } else {
                            cardViewUpgrade.visibility = View.GONE
                        }
                    }
                    armyGroupUpgradeStatus = detail.armyGroupUpgradeStatus
                    if (detail.armyGroupUpgradeStatus) {
                        cardViewUpgrade.setCardBackgroundColor(ColorUtils.getColor(R.color.app_theme))
                        tvUpgrade.setTextColor(ColorUtils.getColor(R.color.white))
                    } else {
                        cardViewUpgrade.setCardBackgroundColor(ColorUtils.getColor(R.color.colorE8E8E8))
                        tvUpgrade.setTextColor(ColorUtils.getColor(R.color.color999))
                    }

                    detail.levelDesc?.let { level ->
                        lelveH5 = level.lelveH5
                        level.current?.let { current ->
                            tvCurrentName.text = current.name
                            tvCurrentMemberLimit.text = current.memberLimit
                            tvCurrentAuth.text = current.auth
                        }
                        level.next?.let { next ->
                            tvNextName.text = next.name
                            tvNextMemberLimit.text = next.memberLimit
                            tvNextSignInGold.text = next.signInGold
                            tvNextAuth.text = next.auth
                        }
                        level.condition?.let { condition ->
                            tvConditionMemberLimit.text = condition.memberLimit + "人"
                            tvConditionConsumeGold.text = condition.consumeGold
                            if (condition.memberStatus) {
                                tvConditionMemberStatus.text = "已达成"
                            } else {
                                tvConditionMemberStatus.text = "未达成"
                            }
                            if (condition.goldStatus) {
                                tvConditionGoldStatus.text = "已达成"
                            } else {
                                tvConditionGoldStatus.text = "未达成"
                            }

                        }
                    }
                }
            } else {
                ToastUtils.showShort(result.msg)
            }

        }
    }


    override fun handleArmyUpgradeSuccess(result: BaseBean<ArmyUpgrade?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                LiveEventBus.get(Config.EventBus.armyUpgradeSuccess).post(true)
                result.data?.let {
                    val bundle = Bundle()
                    bundle.putString(Config.Extra.level, it.levelStr)
                    bundle.putString(Config.Extra.desc, it.desc)
                    armyUpgradeSuccessDialog.arguments = bundle
                    armyUpgradeSuccessDialog.show(supportFragmentManager)
                    mPresenter?.detail(toUid, gid)
                }

            }
            ToastUtils.showShort(result.msg)
        }
    }

}